layui.use(['form','laytpl', 'element'], function() {
	var form = layui.form;
	var element = layui.element;
	var laytpl = layui.laytpl;

	function GetRequest() {
		var url = location.search; //获取url中"?"符后的字串
		console.log(url);
		var theRequest = new Object();
		if (url.indexOf("?") != -1) {
			var str = url.substr(1);
			strs = str.split("&");
			for (var i = 0; i < strs.length; i++) {
				theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
			}
		}
		return theRequest;
	}
	var id = GetRequest().id

	function getjbxx(id) {
		var param = JSON.stringify({
			"body": {
				"patientId": id,
				"departmentId": 60
			},
			"header": {},
			"token": {}
		});

		$.ajax({
			'type': 'post',
			'url': '/service-patient/patient/viewPatientBasicInfo',
			'data': param,
			'dataType': 'json',
			'contentType': "application/json",
			'success': function(data) {
				console.log(data);
				// $("input[name='bedCode']").val(data.data.bedCode);
				// $("input[name='birthday']").val(data.data.birthday);
				// $("input[name='diatag']").val(data.data.diatag);
				var obj = data.data;
				Object.keys(obj).forEach(function(key) {
					$("input[name=" + key + "]").val(obj[key]);
				});
				if (data.status == 0) {
					//责任护士
					var option = '';
					for (var i = 0; i < data.data.nurseList.length; i++) {
						option += '<option value="' + data.data.nurseList[i].id + '">' + data.data.nurseList[i].name + '</option>'
					}
					$("#nurseList").append(option);

					//主管医生
					var doctor = '';
					for (var i = 0; i < data.data.doctorList.length; i++) {
						doctor += '<option value="' + data.data.doctorList[i].id + '">' + data.data.doctorList[i].name + '</option>'
					}
					$("#doctorList").append(doctor);
					$("#sjdoctor").append(doctor);
					$("#zrdoctor").append(doctor);

					//病情
					var illness = '';
					for (var i = 0; i < data.data.illnessStateList.length; i++) {
						illness += '<option value="' + data.data.illnessStateList[i] + '">' + data.data.illnessStateList[i] +
							'</option>'
					}
					$("#illnessStateList").append(illness);

					//血型
					var bloodType = '';
					for (var i = 0; i < data.data.bloodTypeList.length; i++) {
						bloodType += '<option value="' + data.data.bloodTypeList[i] + '">' + data.data.bloodTypeList[i] +
							'</option>'
					}
					$("#bloodTypeList").append(bloodType);

					form.render('select')
				}

				// 设置Select默认值	
				// $("#eventLevel option[value='3']").attr("selected","selected");

			},
			'error': function(res) {
				console.log(res);
			}
		});
	}
	getjbxx(id)

	// 更新基本信息
	form.on('submit(test)', function(data) {
		// console.log(data.field);
		var param = JSON.stringify({
			"body": {
				"patientId": data.field.patientId,
				"admid": data.field.admid,
				"dateDelivery": "2019-06-28"
			},
			"header": {},
			"token": {}
		});
		$.ajax({
			'type': 'post',
			'url': '/service-patient/patient/updatePatientBasicInfo',
			'data': param,
			'dataType': 'json',
			'contentType': "application/json",
			'success': function(data) {
				console.log(data);
			},
			'error': function(res) {
				console.log(res);
			}
		});
		return false;
	});

	//获取检验报告单
	$.ajax({
		'type': 'post',
		'url': '/service-patient/report/viewInspection',
		'data': JSON.stringify({
			"body": {
				"patientId": id
			},
			"header": {},
			"token": {}
		}),
		'dataType': 'json',
		'contentType': "application/json",
		'success': function(data) {
			console.log(data);
			var getTpl = demo.innerHTML,
				view = document.getElementById('view');
			laytpl(getTpl).render(data, function(html) {
				view.innerHTML = html;
			});
		},
		'error': function(res) {
			console.log(res);
		}
	});
})
